﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace DAO
{
    public class MonAnDAO
    {
        public List<MonAnDTO> LayDanhSachMonAn()
        {
            List<MonAnDTO> listMA = new List<MonAnDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select * from MonAn";

                conn = DataProvider2.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    MonAnDTO maDTO = new MonAnDTO();
                    maDTO.ID_MonAn = reader.GetInt32(0);
                    maDTO.TenMonAn = reader.GetString(1);
                    maDTO.Gia = reader.GetInt32(2);

                    listMA.Add(maDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listMA;
        }
        public List<MonAnDTO> LayGiaTheoMaMonAn(int mamonan)
        {
            List<MonAnDTO> listMA = new List<MonAnDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select * from MonAn where ID_MonAn = " + mamonan + "";

                conn = DataProvider2.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    MonAnDTO maDTO = new MonAnDTO();
                    maDTO.ID_MonAn = reader.GetInt32(0);
                    maDTO.TenMonAn = reader.GetString(1);
                    maDTO.Gia = reader.GetInt32(2);

                    listMA.Add(maDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listMA;
        }

        public List<MonAnDTO> LayDanhSachMonAnTheoMa(int ma)
        {
            List<MonAnDTO> listMA = new List<MonAnDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select * from MonAn Where ID_MonAn = " + ma;

                conn = DataProvider2.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    while (reader.Read())
                    {
                        MonAnDTO maDTO = new MonAnDTO();
                        maDTO.ID_MonAn = reader.GetInt32(0);
                        maDTO.TenMonAn = reader.GetString(1);
                        maDTO.Gia = reader.GetInt32(2);

                        listMA.Add(maDTO);
                    }
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listMA;
        }
    }
}
